package com.wb.web.controller.materials;

import com.wb.common.annotation.Anonymous;
import com.wb.common.core.controller.BaseController;
import com.wb.common.core.domain.AjaxResult;
import com.wb.common.core.page.TableDataInfo;
import com.wb.common.utils.StringUtils;
import com.wb.common.vo.DeleteRequest;
import com.wb.system.domain.MaterialsEntity;
import com.wb.system.domain.PositionModel;
import com.wb.system.service.MaterialsService;
import com.wb.common.vo.MaterialsModelVO;
import com.wb.web.controller.position.vo.PositionModelCreateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 学习资料
 */
@RestController
@RequestMapping("materials")
@Api(tags = "学习资料")
public class MaterialsController extends BaseController {
    @Autowired
    private MaterialsService materialsService;

    @PostMapping("/list")
    @ApiOperation("学习资料列表")
    public TableDataInfo list(@RequestBody MaterialsModelVO materialsModelVO) {
        startPage();
        List<MaterialsEntity> list = materialsService.selectMaterialsList(materialsModelVO);
        return getDataTable(list);
    }

    @PostMapping("/{id}/download")
    @ApiOperation("下载学习资料")
    public Object download(@PathVariable("id") String id) {

        return null;
    }

    @GetMapping("/{id}")
    @ApiOperation("获取资料详情")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(materialsService.selectById(id));
    }

    @PostMapping("/add")
    @ApiOperation("资料创建")
    public AjaxResult add(@Valid @RequestBody MaterialsEntity materialsModelVO) {
        MaterialsEntity materialsEntity = new MaterialsEntity();
        BeanUtils.copyProperties(materialsModelVO, materialsEntity);
        materialsService.saveMaterials(materialsEntity);
        return AjaxResult.success();
    }


    @PutMapping("/update")
    @ApiOperation("更新职位")
    public AjaxResult update(@RequestBody MaterialsEntity materialsModelVO) {
        MaterialsEntity materialsEntity = new MaterialsEntity();
        BeanUtils.copyProperties(materialsModelVO, materialsEntity);
        materialsEntity.setUpdateBy(getUsername());
        materialsService.updateMaterialsById(materialsEntity);
        return AjaxResult.success();
    }

    @DeleteMapping("/deleteAll")
    @ApiOperation("删除All")
    public AjaxResult deleteAll(@RequestBody DeleteRequest request) {
        materialsService.removeMaterialsByIds(request.getIds());
        return AjaxResult.success();
    }


}
